package com.deyuanyun.pic.ctrl.service.pushmsg;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspInsedJointExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspSaalAnodeExcel;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  腐蚀控制---缘装置检测记录/牺牲阳极检测记录数据录入消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-24
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class ExinspInsedJointSaalAnodePushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExinspInsedJointSaalAnodePushMsg.class);

    private String createRemark = "绝缘接头检测结果录入消息推送";// 创建备注

    public static enum InsedJointSaalAnodeEnum {
        INSEDJOINT, //缘装置检测记录
        SAALANODE; //牺牲阳极检测记录
    }

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        String type = (String) super.interfaceParam.get(CtrlPushMsgCons.TYPE_KEY);
        Object obj = super.interfaceParam.get(CtrlPushMsgCons.LIST_KEY);

        if(obj != null && (obj instanceof List)) {
            BizMsgThresholdVO msgThresholdVO = null;
            if(InsedJointSaalAnodeEnum.INSEDJOINT.toString().equals(type)) { //缘装置检测记录
                msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_INSULATION_JOINT_INPUT.getValue());
            }
            if(InsedJointSaalAnodeEnum.SAALANODE.toString().equals(type)) { //牺牲阳极检测记录
                msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_SACRIFICIAL_ANODE_INPUT.getValue());
            }

            if(msgThresholdVO == null) {
                LOGGER.info("没有查询到阀值信息");
                return;
            }

            List list = (List) obj;
            for (int i = 0; i < list.size(); i++) {
                String pipeLineId = ""; //管道id
                String roomName = ""; //场站阀室名字 ,当type为牺牲阳极时为设备编号
                String abnormal = ""; //是否有异常，为YES/NO
                if(InsedJointSaalAnodeEnum.INSEDJOINT.toString().equals(type)) { //缘装置检测记录
                    PicCtrlExinspInsedJointExcel jointExcel = (PicCtrlExinspInsedJointExcel) list.get(i);
                    abnormal = jointExcel.getAbnormal();
                    pipeLineId = jointExcel.getPipeLineId();
                    roomName = PipeDictUtils.getRoomNameById(jointExcel.getPipeValveroomId());
                }
                if(InsedJointSaalAnodeEnum.SAALANODE.toString().equals(type)) { //牺牲阳极检测记录
                    PicCtrlExinspSaalAnodeExcel anodeExcel = (PicCtrlExinspSaalAnodeExcel) list.get(i);
                    abnormal = anodeExcel.getAbnormal();
                    pipeLineId = anodeExcel.getPipeLineId();
                    roomName = anodeExcel.getCustomNumber();
                }

                if("YES".equals(abnormal) && StringUtils.isNotEmpty(roomName) && StringUtils.isNotEmpty(pipeLineId)) {
                    String pipeLineName = PipeDictUtils.getLineById(pipeLineId).getName();
                    super.thresListCastString(Lists.newArrayList(pipeLineName, roomName));
                    this.createRemark = "缘装置检测结果录入消息推送";
                    if(InsedJointSaalAnodeEnum.SAALANODE.toString().equals(type)) {
                        this.createRemark = "牺牲阳极检测结果录入消息推送";
                    }
                    initializeBizMsgSends(msgThresholdVO);
                }
            }
        }
    }

    @Override
    public void initializeBizMsgSends(Object... args) {
        BizMsgThresholdVO msgThresholdVO = (BizMsgThresholdVO) args[0];
        super.roleList = msgThresholdVO.getRoleIdList();
        String msgContent = msgThresholdVO.getMsgContent();

        if (super.roleList != null && !super.roleList.isEmpty()) {
            Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds(roleList); //用户id集合
            List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();

            for (String userId : userIds) {
                BizMsgUser bizMsgUser = new BizMsgUser();
                if (StringUtils.isNotEmpty(msgContent))
                    bizMsgUser.setMsgContent(msgContent);
                if (StringUtils.isNotEmpty(super.thresholdContent)) { //设置阀值内容
                    bizMsgUser.setThresholdContent(super.thresholdContent);
                }
                bizMsgUser.setCreateRemark(this.createRemark);
                bizMsgUser.setCreateDate(new Date());
                bizMsgUser.setUserId(userId);
                bizMsgUser.setId(CommonUtil.getUUID());

                userInfos.add(msgQueue.new BizMsgUserInfo().setValid(false).setBizMsgUser(bizMsgUser));
            }
            if(!userInfos.isEmpty()) {
                //封装好需要发送的消息信息交给队列来推送消息
                MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(msgThresholdVO.getId())
                        .setThresholdSet(msgThresholdVO.getThresholdContent()).setRoleIds(super.roleList);
                msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
            }
        }
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
